<?php

/**
 * 门店信息表
 * Class ShopEntityModel
 */
class ShopEntityModel extends BaseModel
{
    protected $table = 'yf_shop_entity';

    protected $principal = 'entity_id';

    const STATUS = [
        'open' => 1,
        'close' => -1
    ];

    public function getNearestByLocation(float $latitude, float $longitude)
    {
        $sql = "select entity_name,ROUND(6378.138 * 2 * ASIN( SQRT( POW( SIN( ( :latitude * PI() / 180 - lat * PI() / 180 ) / 2 ), 2 ) + COS(:latitude * PI() / 180) * COS(lat * PI() / 180) * POW( SIN( ( :longitude * PI() / 180 - lng* PI() / 180 ) / 2 ), 2 ) ) ) * 1000 ) AS distance from {$this->table} where  status=:status order by distance asc limit 1";
        return $this->db->query($sql, [
            ':latitude' => $latitude,
            ':longitude' => $longitude,
            ':status' => self::STATUS['open'],
        ])->fetch(PDO::FETCH_ASSOC);
    }
}